<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>充值卡管理</title>
    <link rel="stylesheet" type="text/css" href="/themes/default/easyui.css" id="swicth-style">
    <link rel="stylesheet" type="text/css" href="/css/icon.css">
    <link rel="stylesheet" type="text/css" href="/css/common.css">
    <link rel="stylesheet" type="text/css" href="/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="/themes/color.css">
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script type="text/javascript" src="/js/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/js/jquery.cookie.js"></script>
    <script type="text/javascript" src="/js/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/js/common.js"></script>
</head>

<body class="easyui-layout" style="padding:5px;">
<fieldset>
    <legend>信息查询</legend>
    <form id="ffSearch" method="post">
        <div id="searchBar">
            <table cellspacing="0" cellpadding="0">
                <tr>
                    <th>充值卡名称：</th>
                    <td style="width:120px">
                        <input name="name" class="easyui-textbox" type="text" style="width:100px">
                    </td>

                    <th>充值卡状态：</th>
                    <td style="width:120px">
                        <select id="statusSelect" name="status" class="easyui-combobox" data-options="onSelect: function(rec){onSearch()}" style="width:100px">
                            <option value="0">全部</option>
                            <option value="1">初始化</option>
                            <option value="2">发布</option>
                            <option value="3">禁用</option>
                        </select>
                    </td>
                    <td colspan="2">
                        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="onSearch()">查询</a>&nbsp;&nbsp;
                        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-clear'" onclick="onClear()">重置</a>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</fieldset>

<table id="dg"> </table>

<div id="addOrEdit" class="easyui-window" title="添加充值卡" data-options="closed:true,iconCls:'icon-save',footer:'#ft'" style="width:450px;height:300px;padding:10px;">
    <form id="ff" method="post">
        <table class="table-blue">
            <tr>
                <th>充值卡名称：</th>
                <td><input type="hidden" name="id"/><input class="easyui-validatebox textbox" type="text" name="name" data-options="required:true, validType:'maxLength[50]'"/></td>
            </tr>
            <tr>
                <th>充值金额：</th>
                <td><input class="easyui-validatebox textbox" type="text" id="faceValue" name="faceValue" data-options="required:true, validType:'maxLength[20]'"/></td>
            </tr>
            <tr>
                <th>赠送金额：</th>
                <td><input class="easyui-validatebox textbox" type="text" id="freeValue" name="freeValue" data-options="required:true, validType:'maxLength[20]'"/></td>
            </tr>
            <tr>
                <th>失效日期：</th>
                <td><input  type="text" class="easyui-datebox" editable="false"  name="expirationDate" data-options="required:true, validType:'maxLength[20]'"/></td>
            </tr>

            <tr>
                <th>备注：</th>
                <td><input class="easyui-validatebox textbox" name="remarks" data-options="multiline:true, validType:'maxLength[60]'" style="height:60px;"/></td>
            </tr>
        </table>
    </form>
    <div id="ft" style="padding:5px;text-align: right;">
        <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" onclick="submitForm()" style="width:80px">Ok</a> &nbsp;&nbsp;
        <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" onclick="closeWindow()" style="width:80px">Cancel</a>
    </div>
</div>



<script type="text/javascript">
    $(function(){
        $("#statusSelect").combobox("setValue","2");//默认显示已发布的充值卡
        onSearch();
        var dg = $('#dg').datagrid();
        dg.datagrid({
            url:'/v1/api0/equipmentConfig/rechargeCardList',
            columns:[[
                {field:'ck',checkbox:true},
                {field:'id',hidden:true,title:'充值卡ID',width:100},
                {field:'name',title:'充值卡名称',width:100},
                {field:'faceValue',title:'充值金额',width:100},
                {field:'freeValue',title:'赠送金额',width:100},
                {field:'createTime',title:'创建时间',width:100,formatter: formatDateTime},
                {field:'expirationDate',title:'失效日期',width:100,formatter: formatDate},
                {field:'status',title:'状态',width:100,formatter: function(value,row,index){
                    switch(value) {
                        case 1: return "初始化";
                        case 2: return "发布";
                        case 3: return "禁用";
                    }
                }},
                {field:'action',title:'操作',width:100,formatter: function(value,row,index){
                    if(row.status == 1){
                        return  '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="release(\''+row.id+'\')">发布</a>';

                    }else if(row.status == 2){
                        return  '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="disable(\''+row.id+'\')">禁用</a>';
                    }else{
                        return '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="enable(\''+row.id+'\')">启用</a>';
                    }
                }}
            ]],
            title:'充值卡管理',
            pagination: true,
            iconCls: 'icon-application_view_detail',
            singleSelect: false,
            toolbar: [{
                id: 'btnAdd',
                text: '添加',
                iconCls: 'icon-add',
                handler: function () {
                    add();
                }
            }, '-', {
                id: 'btnEdit',
                text: '修改',
                iconCls: 'icon-edit',
                handler: function () {
                    edit();
                }
            }, '-', {
                id: 'btnReload',
                text: '刷新',
                iconCls: 'icon-reload',
                handler: function () {
                    $("#dg").datagrid("reload");
                }
            }]
        });

        var heightMargin = $("#searchBar").height() + 45;
        $('#dg').resizeDataGrid(heightMargin, 0, 0, 0);
        $(window).resize(function() {
            $('#dg').resizeDataGrid(heightMargin, 0, 0, 0);
        });
    });

    function onSearch(){
        var queryParams = $('#ffSearch').serializeObject();
        $('#dg').datagrid({
            url:'/v1/api0/equipmentConfig/rechargeCardList',
            queryParams: queryParams
        });
    }

    function onClear() {
        $('#ffSearch').form('clear');
        $('#statusSelect').combobox('setValue', '2');
        onSearch();
    }

    function add() {
        $('#ff').form('clear');
        $("#ff input[name='id']").val("");
        $('#addOrEdit').window({title:'添加充值卡'});
        $('#addOrEdit').window('open');
    }

    function edit() {
        $('#ff').form('clear');
        var row = $('#dg').datagrid('getSelected');
        if (!row){
            top.window.showMessage("请选择充值卡");
            return;
        }
        $('#ff').form('load',row);
        $("#ff input[name='id']").val(row.id);
        $('body').data('ff-serialize', $('#ff').serialize());
        $('#addOrEdit').window({title:'编辑充值卡'});
        $('#addOrEdit').window('open');
    }

    function submitForm(){
         var faceValue = $("#faceValue").val();
        var freeValue = $("#freeValue").val();
        if(!checkNumber(faceValue)){
            top.window.showMessage("充值金额只能是正整数");
                return;
        }
        if(!checkNumber(freeValue)){
            top.window.showMessage("赠送金额只能是正整数");
                return;
        }
        var id = $("#ff input[name='id']").val();
        var url = id.length ? "/v1/api0/equipmentConfig/editRechargeCard" : "/v1/api0/equipmentConfig/addRechargeCard";
        var data = $('#ff').serialize();
        if(data == $('body').data('ff-serialize')) {
            top.window.showMessage("数据没有改动");
            return;
        }
        $.ajax({
            type: "post",
            url: url,
            data: data,
            datatype: "json",
            beforeSend: function () {
                if (!$('#ff').form('validate')){
                    return false;
                }
                $('#dg').datagrid('loading');
            },
            success: function (data) {
                if(data.code == 200) {
                    $('#dg').datagrid('reload');
                    top.window.showMessage();
                    closeWindow();
                } else {
                    top.window.showMessage(data.msg);
                    $('#dg').datagrid('loaded');
                }
            },
            error: function () {
                $('#dg').datagrid('loaded');
            }
        });
    }

    function closeWindow(){
        $('#addOrEdit').window('close');
        $('#bind').window('close');
    }


    /* function disable(id){
         alert("============"+id);
         var url = "/v1/api0/equipmentConfig/disableTerminal";
         var data = {id: id};
         $.ajax(ajaxOption(url, data));
     }*/

    function disable(id) {
    top.window.$.messager.confirm('确认', '您确定要禁用该充值卡吗？', function(r){
            if (r){
                 var url = "/v1/api0/equipmentConfig/disableRechargeCard";
                var data = {id: id};
                $.ajax(ajaxOption(url, data));
            }
        });

    }


    function enable(id) {
        top.window.$.messager.confirm('确认', '您确定要启用该充值卡吗？', function(r){
            if (r){
                 var url = "/v1/api0/equipmentConfig/enableRechargeCard";
                var data = {id: id};
                $.ajax(ajaxOption(url, data));
            }
        });
    }

    function release(id) {
        top.window.$.messager.confirm('确认', '您确定要发布该充值卡吗？', function(r){
            if (r){
                 var url = "/v1/api0/equipmentConfig/releaseRechargeCard";
                var data = {id: id};
                $.ajax(ajaxOption(url, data));
            }
        });
    }

    function ajaxOption(url, data) {
        return {
            type: "post",
            url: url,
            data: data,
            datatype: "json",
            beforeSend: function () {
                $('#dg').datagrid('loading');
            },
            success: function (data) {
                if(data.code == 200) {
                    $('#dg').datagrid('reload');
                    top.window.showMessage();
                    closeWindow();
                } else {
                    top.window.showMessage(data.msg);
                    $('#dg').datagrid('loaded');
                }
            },
            error: function () {
                $('#dg').datagrid('loaded');
            }
        }
    }

    function checkNumber(input) {
 　　<!--var re = /^[0-9]+.?[0-9]*$/; //判断字符串是否为数字 -->
     var re = /^[1-9]+[0-9]*]*$/;    //判断正整数
　　if (!re.test(input)) {
 　　　　return false;
 　　}else{
            return true;
        }
 }
</script>
</body>
</html>
